package com.sdo.bambooksdk;

import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class UnpackSnbFile {

    private static String snbFile = ".\\test.snb";
    private static String tempPath = "D:\\temp_test_snb";

    public static void main(String args[]) {
        BBUtil.deleteDir(tempPath);
        String path = tempPath + "\\";
        (new File(path)).mkdir();
        path = tempPath + "\\snbf\\";
        (new File(path)).mkdir();
        path = tempPath + "\\snbc\\";
        (new File(path)).mkdir();
        path = tempPath + "\\snbc\\images\\";
        (new File(path)).mkdir();
        String bookFile = tempPath + "\\snbf\\book.snbf";
        BambookCore.INSTANCE.BambookUnpackFileFromSnb(snbFile, "snbf/book.snbf", bookFile);
        parseBookFile(bookFile);
        String tocFile = tempPath + "\\snbf\\toc.snbf";
        BambookCore.INSTANCE.BambookUnpackFileFromSnb(snbFile, "snbf/toc.snbf", tocFile);
        parseTocFile(tocFile);
    }

    private static void parseBookFile(String filename) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(filename);
            Element root = doc.getDocumentElement();
            NodeList nl = root.getChildNodes();
            for (int i = 0; i < nl.getLength(); i++) {
                Node n = nl.item(i);
                if (n.getNodeName().equals("head")) {
                    NodeList headnl = n.getChildNodes();
                    for (int j = 0; j < headnl.getLength(); j++) {
                        Node hn = headnl.item(j);
                        if (hn.getNodeName().equals("cover")) {
                            if (hn.getChildNodes().getLength() == 1) {
                                String cover = hn.getChildNodes().item(0).getNodeValue();
                                if (cover != null) {
                                    BambookCore.INSTANCE.BambookUnpackFileFromSnb(snbFile, "snbc/images/" + cover, tempPath + "\\snbc\\images\\" + cover);
                                }
                            }
                        }
                    }
                }
            }
        } catch (SAXException ex) {
        } catch (IOException ex) {
        } catch (ParserConfigurationException ex) {
        }
    }

    private static void parseTocFile(String tocFile) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(tocFile);
            Element root = doc.getDocumentElement();
            NodeList nl = root.getChildNodes();
            for (int i = 0; i < nl.getLength(); i++) {
                Node n = nl.item(i);
                if (n.getNodeName().equals("body")) {
                    listChapter(n);
                }
            }
        } catch (SAXException ex) {
        } catch (IOException ex) {
        } catch (ParserConfigurationException ex) {
        }
    }

    private static void listChapter(Node n) {
        NodeList nl = n.getChildNodes();
        for (int i = 0; i < nl.getLength(); i++) {
            Node c = nl.item(i);
            if (c.getNodeName().equals("chapter")) {
                NamedNodeMap attributes = c.getAttributes();
                for (int j = 0; j < attributes.getLength(); j++) {
                    Node item = attributes.item(j);
                    if (item.getNodeName().equals("src")) {
                        String filename = item.getNodeValue();
                        int result = BambookCore.INSTANCE.BambookUnpackFileFromSnb(snbFile, "snbc/" + filename, "D:\\temp_test_snb\\snbc\\" + filename);
                        if (result == Const.BR_SUCC) {
                            parseSnbc("D:\\temp_test_snb\\snbc\\" + filename);
                        }
                    }
                }
            }
        }
    }

    private static void parseSnbc(String filename) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document doc = builder.parse(filename);
            Element root = doc.getDocumentElement();
            NodeList nl = root.getChildNodes();
            for (int i = 0; i < nl.getLength(); i++) {
                Node n = nl.item(i);
                if (n.getNodeName().equals("body")) {
                    NodeList bodylist = n.getChildNodes();
                    for (int j = 0; j < bodylist.getLength(); j++) {
                        Node bodynode = bodylist.item(j);
                        if (bodynode.getNodeName().equals("img")) {
                            if (bodynode.getChildNodes().getLength() == 1) {
                                String img = bodynode.getChildNodes().item(0).getNodeValue();
                                if (img != null) {
                                    BambookCore.INSTANCE.BambookUnpackFileFromSnb(snbFile, "snbc/images/" + img, tempPath + "\\snbc\\images\\" + img);
                                }
                            }
                        }
                    }
                }
            }
        } catch (SAXException ex) {
        } catch (IOException ex) {
        } catch (ParserConfigurationException ex) {
        }
    }
}
